﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace myie.code.pharaoh.com
{
    public class Mouse
    {
        [Flags]
        enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        public static bool SetPos(int x,int y)
        {
            return SetCursorPos(x, y);
        }
        public static void Click()
        {
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
        public static void Click(int x,int y)
        {
            SetCursorPos(x, y);
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
        public static void DClick()
        {
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            System.Threading.Thread.Sleep(50);
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
        public static void DClick(int x,int y)
        {
            SetCursorPos(x, y);
            DClick();
        }   
        public static void RClick()
        {
            mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
        }
        public static void RClick(int x,int y)
        {
            SetCursorPos(x, y);
            RClick();
        }
        public static void MClick()
        {
            mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, UIntPtr.Zero);
        }
        public static void MClick(int x, int y)
        {
            SetCursorPos(x, y);
            MClick();
        }   
        public static void Move(int dx,int dy)
        {
            mouse_event(MouseEventFlag.Move, dx, dy, 0, UIntPtr.Zero);
        }
        public static void LDown()
        {
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
        }
        public static void LUp()
        {
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
        /// <summary>
        /// 从当前鼠标位置拖动到新位置（绝对值）
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void DragTo(int x,int y)
        {
            LDown();
            SetCursorPos(x,y);
            LUp();
        }
        /// <summary>
        /// 从某点拖动鼠标到新的地点（绝对值）
        /// </summary>
        /// <param name="beginX"></param>
        /// <param name="beginY"></param>
        /// <param name="endX"></param>
        /// <param name="endY"></param>
        public static void Drag(int beginX,int beginY,int endX,int endY)
        {
            SetCursorPos(beginX, beginY);
            LDown();
            SetCursorPos(endX, endY);
            LUp();
        }
        /// <summary>
        /// 拖动鼠标到新位置（相对值）
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        public static void Drag(int dx, int dy)
        {
            LDown();
            Move(dx, dy);
            LUp();
        }

    }
}
